﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraCharts;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class TrendsNew : XForm
    {
        string tip;
        public TrendsNew(string _tip)
        {
            InitializeComponent();
            tip = _tip;
            this.Text += "-" + tip;
        }

        //http://documentation.devexpress.com/#corelibraries/clsDevExpressXtraPivotGridPivotGridOptionsChartDataSourcetopic
        //http://www.devexpress.com/Support/Center/Example/Details/E2214

        DataSet d;
        BindingSource bs;
        private void TrendsNew_Load(object sender, EventArgs e)
        {
            
            chartControl1.Series.Add("Toplam", DevExpress.XtraCharts.ViewType.Spline);
            chartControl1.Series.Add("Ayrılan", DevExpress.XtraCharts.ViewType.Spline);
            chartControl1.Series.Add("Idle", DevExpress.XtraCharts.ViewType.Spline);

            //chartControl1.Series.Add("gQty", DevExpress.XtraCharts.ViewType.Bar);
            //chartControl1.Series.Add("cQty", DevExpress.XtraCharts.ViewType.Bar);
            //chartControl1.PaletteName = "Verde";
            //RefreshData();

           

            //BindDataToChart();



            /*            
            chartControl1.Series.Clear();
            chartControl1.DataSource = reportDataSet.Tables[0];
            chartControl1.SeriesDataMember = cbSeriesField.Text;
            chartControl1.SeriesTemplate.View = new SeriesBase(getViewType(cbChartType.Text)).View;
            chartControl1.SeriesTemplate.ArgumentDataMember = cbYAxis.Text;
            chartControl1.SeriesTemplate.ValueDataMembers.Clear();
            chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] { cbXaxis.Text });
                

            //chartControl1.SeriesDataMember = "Tarih";
            //chartControl1.SeriesTemplate.ArgumentDataMember = "Arguments";
            //chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Values" });
             */
            chartControl1.CustomDrawSeriesPoint += chartControl1_CustomDrawSeriesPoint;
            chartControl1.CustomDrawAxisLabel += chartControl1_CustomDrawAxisLabel;
            //chartControl1.BoundDataChanged += chartControl1_BoundDataChanged;

            dateEdit1.EditValueChanged += dateEdit1_EditValueChanged;
            dateEdit1.EditValue = new DateTime(2013, 9, 25);

            gridView1.RowCellStyle += gridView1_RowCellStyle;

            //comboBoxEdit1.SelectedIndexChanged += comboBoxEdit1_SelectedIndexChanged;
            //comboBoxEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            splitContainerControl1.AutoSize();
            gridView1.BestFitColumns();

            checkButton1.CheckedChanged += checkButton1_CheckedChanged;
        }

        void checkButton1_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshData();
            splitContainerControl1.PanelVisibility = checkButton1.Checked ? DevExpress.XtraEditors.SplitPanelVisibility.Panel2 : DevExpress.XtraEditors.SplitPanelVisibility.Both;
       
        }

        private void chartControl1_CustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e)
        {            
            var pointValue = e.SeriesPoint.Values[0];
            e.LabelText = e.SeriesPoint.Argument + " : " + pointValue;
        }

        void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName.StartsWith("Fark") && e.Column.FieldName.EndsWith("Qty"))
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void dateEdit1_EditValueChanged(object sender, System.EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData()
        {
            d = ExecuteDataSet("GetTrends" + tip + "2", dateEdit1.EditValue.ToDate(), checkButton1.Checked);
            if (!checkButton1.Checked)
            {
                bs = gridView1.Init(d.Tables[0]);

                gridView1.SetCaptions("FarkIYuzde:%", "FarkAYuzde:%", "FarkTYuzde:%", "FarkIQty:Fark", "FarkAQty:Fark", "FarkTQty:Fark");
                gridView1.FormatAsNumber(1, "FarkIYuzde", "FarkAYuzde", "FarkTYuzde");
                gridView1.FormatAsNumber(0, "Idle1Qty", "Idle2Qty", "FarkIQty", "Ayrilan1Qty", "Ayrilan2Qty", "FarkAQty", "Toplam1Qty", "Toplam2Qty", "FarkTQty");
                gridView1.AddSummary("Idle1Qty", "Idle2Qty", "FarkIQty", "Ayrilan1Qty", "Ayrilan2Qty", "FarkAQty", "Toplam1Qty", "Toplam2Qty", "FarkTQty");
                gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;

                bs.CurrentChanged -= bs_CurrentChanged;
                bs.CurrentChanged += bs_CurrentChanged;
                gridView1.MakeReadOnly();
                gridView1.BestFitColumns();

            }

            BindDataToChart();
        }

        string[] val = "Toplam;Ayrilan;Idle".Split(";".ToCharArray());  //;gQty;cQty
        private void BindDataToChart()
        {
            /*
            chartControl1.DataSource = null;
            chartControl1.SeriesDataMember = null;

            chartControl1.Series.Clear();
            chartControl1.Series.Add(string.Empty, DevExpress.XtraCharts.ViewType.Spline);
            */

            /*
            if (checkButton1.Checked && tip=="K")
            {
                chartControl1.Series.Add("gQty", ViewType.Bar);
                chartControl1.Series.Add("cQty", ViewType.Bar);
                //chartControl1.Series.Add("tQty", DevExpress.XtraCharts.ViewType.Spline);

                //((BarSeriesView)chartControl1.Series["gQty"]).BarWidth =
                //((BarSeriesView)chartControl1.Series["cQty"]).BarWidth = 0.25;
            }
            else
            {
                if (chartControl1.Series.Contains(chartControl1.Series["gQty"]))
                    chartControl1.Series.Remove(chartControl1.Series["gQty"]);

                if (chartControl1.Series.Contains(chartControl1.Series["cQty"]))
                    chartControl1.Series.Remove(chartControl1.Series["cQty"]);

                //if (chartControl1.Series.Contains(chartControl1.Series["tQty"]))
                //    chartControl1.Series.Remove(chartControl1.Series["tQty"]);
            }
            */
            DataTable dt;
            int seri = 0;
            if (!checkButton1.Checked)
                dt = d.Tables["Table1"].Select("CustomerId=" + intParse(bs, "CustomerId")).CopyToDataTable();
            else
                dt = d.Tables["Table"];
                
            foreach (string str in val)
            {
                if (seri >= 3 && !(checkButton1.Checked && tip == "K"))
                    continue;

                chartControl1.Series[seri].DataSource = dt;

                chartControl1.Series[seri].ValueDataMembers.Clear();
                //if (comboBoxEdit1.SelectedIndex > -1)
                chartControl1.Series[seri].ValueDataMembers.AddRange(str);
                chartControl1.Series[seri].ArgumentDataMember = "Tarih";

                seri++;
            }
            
            /*
            DataTable dt = d.Tables["Table1"].Select("CustomerId=" + intParse(bs, "CustomerId")).CopyToDataTable();
            chartControl1.Series[0].DataSource = dt;

            chartControl1.Series[0].ValueDataMembers.Clear();
            if (comboBoxEdit1.SelectedIndex > -1)
                chartControl1.Series[0].ValueDataMembers.AddRange(val[comboBoxEdit1.SelectedIndex]);
            chartControl1.Series[0].ArgumentDataMember = "Tarih";
            */


            //chartControl1.Series.Clear();
            //chartControl1.DataSource = d.Tables["Table1"].Select("CustomerId=" + intParse(bs, "CustomerId")).CopyToDataTable();
            //chartControl1.SeriesDataMember = "CustomerId";
            //chartControl1.SeriesTemplate.View = new SeriesBase(DevExpress.XtraCharts.ViewType.Spline).View;
            //chartControl1.SeriesTemplate.ArgumentDataMember = "Tarih";
            //chartControl1.SeriesTemplate.ValueDataMembers.Clear();
            //chartControl1.SeriesTemplate.ValueDataMembers.AddRange(val);

            ((XYDiagram)chartControl1.Diagram).AxisX.Tickmarks.CrossAxis = true;
            //((XYDiagram)chartControl1.Diagram).AxisX.GridSpacingAuto = true;
        }

        void bs_CurrentChanged(object sender, System.EventArgs e)
        {
            BindDataToChart();
        }

        //void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    BindDataToChart();
        //}

        void chartControl1_BoundDataChanged(object sender, EventArgs e)
        {
            //SetDateFormat();
        }

        void chartControl1_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {
            
            if (e.Item.AxisValue.GetType() == typeof(DateTime))
                e.Item.Text = ((DateTime)e.Item.AxisValue).ToString("dd.MM.yyyy");
            else if (e.Item.AxisValue.GetType() == typeof(double))
                e.Item.Text = ((double)e.Item.AxisValue).ToString("#,###");
        }

        //private void SetDateFormat()
        //{
        //    return;
        //    (chartControl1.Diagram as XYDiagram).AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Day;
        //    (chartControl1.Diagram as XYDiagram).AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day;
        //   // (chartControl1.Diagram as XYDiagram).AxisX.Label.DateTimeOptions.Format = DateTimeFormat.ShortTime;
        //    (chartControl1.Diagram as XYDiagram).AxisX.Label.DateTimeOptions.Format = DateTimeFormat.ShortDate;
        //}

    }
}
